package com.blb.servlet.admin;

import com.alibaba.fastjson.JSONArray;
import com.blb.pojo.*;
import com.blb.service.admin.IProductService;
import com.blb.service.admin.IProductTypeService;
import com.blb.service.admin.impl.ProductServiceImpl;
import com.blb.service.admin.impl.ProductTypeServiceImpl;
import com.blb.servlet.BaseServlet;
import com.blb.utils.ApplicationContextUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet(value = "/admin/product/*", loadOnStartup = 1)
public class ProductServlet extends BaseServlet {

    private IProductService productService = (IProductService) ApplicationContextUtil.getBean(IProductService.class);
    private IProductTypeService productTypeService = (IProductTypeService) ApplicationContextUtil.getBean(IProductTypeService.class);


    /**
     * 分页
     * @param req
     * @param resp
     */
    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String name = req.getParameter("name");
        int page = getPage();
        int size = getSize();

        try {
            Page<Product> page1 = productService.page(page, size, name);
            req.setAttribute("name", name);
            req.setAttribute("page", page1);
            forward("/admin/view/product/list");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }

    /**
     * 添加商品
     * @param req
     * @param resp
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            Product product = getEntity(Product.class);
            product.setCreateTime(LocalDateTime.now());
            product.setStatus(0);
            productService.add(product);
            writeJson(Result.ok("添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("添加失败："+e.getMessage()));
        }
    }

    /**
     * 删除
     * @param req
     * @param resp
     * @throws IOException
     */
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            productService.deleteById(Integer.parseInt(id));
            writeJson(Result.ok("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("删除失败："+e.getMessage()));
        }
    }

    /**
     * 批量删除
     * @param req
     * @param resp
     * @throws IOException
     */
    public void batchDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String ids = req.getParameter("ids");
            String[] idArr = JSONArray.parseArray(ids).toArray(new String[0]);
            productService.batchDelete(idArr);
            writeJson(Result.ok("批量删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("批量删除失败："+e.getMessage()));
        }
    }

    /**
     * 跳转添加界面
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    public void addPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        try {
            List<ProductType> list = productTypeService.all();
            req.setAttribute("types", list);
            forward("/admin/view/product/add");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }

    /**
     * 跳转修改界面
     * @param req
     * @param resp
     * @throws IOException
     */
    public void updatePage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        try {
            Product product = productService.getById(Integer.parseInt(id));
            List<ProductType> list = productTypeService.all();

            req.setAttribute("types", list);
            req.setAttribute("product", product);
            forward("/admin/view/product/update");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }

    /**
     * 修改商品
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        try {
            Product product = getEntity(Product.class);
            productService.update(product);
            writeJson(Result.ok("修改成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("修改失败："+e.getMessage()));
        }
    }

    /**
     * 上下架
     * @param req
     * @param resp
     */
    public void changeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            String status = req.getParameter("status");
            productService.changeStatus(Integer.parseInt(id), Integer.parseInt(status));
            if("1".equals(status)){
                writeJson(Result.ok("上架成功"));
            }else if("0".equals(status)){
                writeJson(Result.ok("下架成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.ok("操作失败："+e.getMessage()));
        }
    }
}
